package com.tool.emulator.mos6502.instrs;

import com.tool.emulator.common.Util;

/**
 * AddressingMethod.java @author tl 2011-6-16
 */
public enum AddressingMethod {
	Accumulator,
	Immediate,
	Zero_Page,
	Zero_Page_X,
	Zero_Page_Y,
	Absolute,
	Absolute_X,
	Absolute_Y,
	Implied,
	Relative,
	Indirect_X,
	Indirect_Y,
	Indirect;

	public String dump(int pc, int op) {
		int oplen = this.getOpLen();
		String ops;
		if (oplen == 1) return null;
		if (oplen == 2)
			ops = "$" + Util.b2s(op);
		else
			ops = "$" + Util.w2s(op);
		switch (this) {
			case Accumulator:
			case Implied:
				return null;
			case Immediate:
				return "#" + ops;
			case Zero_Page:
			case Absolute:
				return ops;
			case Zero_Page_X:
			case Absolute_X:
				return ops + ",X";
			case Zero_Page_Y:
			case Absolute_Y:
				return ops + ",Y";
			case Relative:
				return "$" + Util.w2s(pc + (byte) op);
			case Indirect_X:
				return "(" + ops + ",X)";
			case Indirect_Y:
				return "(" + ops + "),Y";
			case Indirect:
				return "(" + ops + ")";
		}
		throw new Error();
	}

	public String getMethodName() {
		switch (this) {
			case Accumulator:
				return "acc";
			case Implied:
				return null;
			case Immediate:
				return "imm";
			case Zero_Page:
				return "zpg";
			case Zero_Page_X:
				return "zpgx";
			case Zero_Page_Y:
				return "zpgy";
			case Relative:
				return "rel";
			case Indirect_X:
				return "ix";
			case Indirect_Y:
				return "iy";
			case Absolute:
				return "abs";
			case Absolute_X:
				return "absx";
			case Absolute_Y:
				return "absy";
			case Indirect:
				return "ind";
			default:
				return null;
		}
	}

	public int getOpLen() {
		switch (this) {
			case Accumulator:
			case Implied:
				return 1;
			case Immediate:
			case Zero_Page:
			case Zero_Page_X:
			case Zero_Page_Y:
			case Relative:
			case Indirect_X:
			case Indirect_Y:
				return 2;
			case Absolute:
			case Absolute_X:
			case Absolute_Y:
			case Indirect:
				return 3;
		}
		return 0;
	}
}
